// pages/music/music.js
const MusicPlayer = wx.createInnerAudioContext();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    musicList: [{
      id: 0,
      title: "伤心太平洋",
      auth: "左傲雪",
      source: "https://zjy.mmifx.com/source/music/sxtpy.m4a",
      img: "http://img.mmifx.com/miniapp/20210608210353.png"
    }, {
      id: 1,
      title: "阿拉斯加的海湾",
      auth: "蓝心羽",
      source: "https://zjy.mmifx.com/source/music/alsjdhw.mp3",
      img: "http://img.mmifx.com/miniapp/20210608210913.png"
    }, {
      id: 2,
      title: "城南花已开",
      auth: "三亩地",
      source: "https://zjy.mmifx.com/source/music/cnhyk.m4a",
      img: "http://img.mmifx.com/miniapp/20210608210942.png"
    }, {
      id: 3,
      title: "错位时空",
      auth: "艾辰",
      source: "https://zjy.mmifx.com/source/music/cwsk.mp3",
      img: "http://img.mmifx.com/miniapp/20210608211008.png"
    }, {
      id: 4,
      title: "飞鸟和蝉",
      auth: "任然",
      source: "https://zjy.mmifx.com/source/music/cnhc.m4a",
      img: "http://img.mmifx.com/miniapp/20210608211226.png"
    }],
    nowMusic: {
      id: 3,
      title: "错位时空",
      auth: "艾辰",
      duration: 0,
      process: 0,
      source: "https://zjy.mmifx.com/source/music/cwsk.mp3",
      img: "http://img.mmifx.com/miniapp/20210608211008.png",
      total: "03:21",
      nowtime: "00:00",
      pause: false
    }
  },
  // 播放器初始化
  RandMusic() {
    let that = this;
    var rand = Math.floor(Math.random() * 4)
    that.setData({
      nowMusic: that.data.musicList[rand]
    })
    that.PlayerInit()
  },
  PlayerInit() {
    let that = this;
    MusicPlayer.src = that.data.nowMusic.source
    MusicPlayer.onCanplay(e => {
      that.setData({
        'nowMusic.duration': MusicPlayer.duration
      })
    })
    MusicPlayer.play()
    MusicPlayer.onPlay(() => {
      console.log("音乐开始播放");
      this.setData({
        'nowMusic.pause': false
      })
    })
    MusicPlayer.onTimeUpdate((e) => {
      let that = this;
      if (that.data.nowMusic.duration != MusicPlayer.duration && MusicPlayer.duration != NaN) {
        that.setData({
          'nowMusic.duration': MusicPlayer.duration,
          'nowMusic.total': that.ParseTime(MusicPlayer.duration)
        })
      }
      that.setData({
        'nowMusic.nowtime': that.ParseTime(MusicPlayer.currentTime),
        'nowMusic.process': Math.floor(MusicPlayer.currentTime / MusicPlayer.duration * 100)
      })
    })
    MusicPlayer.onError(e => {
      console.log("error : ", e);
    })
    MusicPlayer.onPause(() => {
      console.log("音乐被暂停了");
      this.setData({
        'nowMusic.pause': true
      })
    })
  },
  // 单击暂停事件
  pause() {
    let that = this;
    console.log("单击了暂停/播放");
    if (MusicPlayer.paused) {
      MusicPlayer.play()
    } else {
      MusicPlayer.pause()
    }
  },
  ParseTime(num) {
    num = Math.floor(num);
    var min = Math.floor(num / 60);
    var s = num % 60;
    if (min < 10) {
      min = "0" + min;
    }
    if (s < 10) {
      s = "0" + s;
    }
    return min + ":" + s
  },
  nextMusic() {
    this.RandMusic()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.RandMusic()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})